package com.blog.controller;

import java.io.IOException;
import java.util.List;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import com.blog.BO.DiscussBO;
import com.blog.dao.DiscussDao;
import com.blog.dao.impl.DiscussDaoImpl;
import com.blog.entity.Discuss;
import com.blog.entity.Page;
import com.fasterxml.jackson.databind.ObjectMapper;

@WebServlet("/disscusById.do")
public class DisscusByIdServlet extends HttpServlet {
	
	protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		// TODO Auto-generated method stub
		int pageNum = request.getParameter("pageNum") == null ? 1 : Integer.parseInt(request.getParameter("pageNum"));
		int pageSize = request.getParameter("pageSize") == null ? 6 : Integer.parseInt(request.getParameter("pageSize"));
		int postId = request.getParameter("postId") == null ? 1 : Integer.parseInt(request.getParameter("postId"));
		response.setContentType("application/json;charset=utf-8");
		DiscussBO db= new DiscussBO();
		Discuss dc=new Discuss();
		dc.setPostId(postId);
		db.setPostId(dc.getPostId());
		DiscussDao dd= new DiscussDaoImpl();
		List<Discuss> data= dd.selectDiscussByDiscussBO(db, pageNum, pageSize);
		int total=dd.selectNum(postId);
		new ObjectMapper().writeValue(response.getWriter(),new Page<Discuss>(data,pageNum,pageSize,total));
	}

	protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		// TODO Auto-generated method stub
		doGet(request, response);
	}

}
